import { GCellRenderer } from "../../../../../../com/mui/controls/GCellRenderer";
import { ObjCreate } from "../../../common/ObjCreate";
import { ImagesConst } from "../../../../resource/ImagesConst";
import { ThingUtil } from "../../../../scene/layer/utils/ThingUtil";
import { SGuildGroupPlayer } from "../../../../../../Message/Public/SGuildGroupPlayer";
import { MsgManager } from "../../../../manager/MsgManager";
import { Language } from "../../../../../../extend/language/Language";
import { CareerUtil } from "../../../common/util/CareerUtil";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { EventName } from "../../../../mvc/EventName";
import { EntityUtil } from "../../../../scene/layer/utils/EntityUtil";
import { Cache } from "../../../../cache/Cache";
import { RolePlayer } from "../../../../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../../../../scene/events/PlayerEvent";
import { HTMLUtil } from "../../../../../../com/gengine/utils/HTMLUtil";
import { GlobalStyle } from "../../../../../common/global/GlobalStyle";
import { DataEvent } from "../../../../events/DataEvent";
import { SEntityInfo } from "../../../../../../Message/BroadCast/SEntityInfo";
import { SEntityId } from "../../../../../../Message/Public/SEntityId";
import { IEntity } from "../../../../scene/player/IEntity";
import { GTextFiled } from "../../../../../../com/mui/controls/GTextFiled";
//class GuildEliteGroupPanelGroupRenderer
    
    export  class GuildEliteGroupPanelGroupRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            ObjCreate.createBitmap(ImagesConst.GreenBloodBg, 3, 2, this);
            this._greenBloodBm = ObjCreate.createBitmap(ImagesConst.GreenBlood, 4, 3, this);
            this._name = ObjCreate.createGTextField("", 5, 3, 65, 20, this);
            this._name.mouseEnabled = false;
            this._blood = ObjCreate.createGTextField("", 70, 3, 50, 20, this);
            this._blood.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._blood.mouseEnabled = false;
            this._careerBm = new Object /* flash.display.Bitmap */();
            this._careerBm.x = 100;
            this._careerBm.y = 1;
            this.addChild(this._careerBm);
            this.addEventListener(flash.events.MouseEvent.CLICK, this.onMouseClickHandler);
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
            this.buttonMode = true;
            return;
        }

        /* internal  */onMouseClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (data) 
            {
                loc1 = ThingUtil.entityUtil.getEntity((data as SGuildGroupPlayer).player.entityId);
                if (loc1) 
                {
                    loc1.selected = true;
                }
                else 
                {
                    if (ThingUtil.selectEntity) 
                    {
                        ThingUtil.selectEntity.selected = false;
                    }
                    MsgManager.showRollTipsMsg(Language.getStringByParam(60684, (data as SGuildGroupPlayer).player.name));
                }
            }
            return;
        }

        /* internal  */onAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            this.bindEntity();
            this.addListener();
            return;
        }

        /* internal  */onRemovedFromStage(arg1: Object /* flash.events.Event */): void
        {
            this.unBindEntity();
            this.removeListener();
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            var loc1=arg1 as SGuildGroupPlayer;
            if (loc1) 
            {
                this._player = loc1;
                this._careerBm.bitmapData = CareerUtil.getCareerIconBmd(loc1.player.careerEnable);
                this.bindEntity();
                this.updateLife();
            }
            return;
        }

        /* internal  */addListener(): void
        {
            Dispatcher.addEventListener(EventName.Group_StepIn, this.onGroupStepInHandler);
            Dispatcher.addEventListener(EventName.Group_StepOut, this.onGroupStepOutHandler);
            Dispatcher.addEventListener(EventName.ChangeScene, this.onGroupStepOutHandler);
            return;
        }

        /* internal  */removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.Group_StepIn, this.onGroupStepInHandler);
            Dispatcher.removeEventListener(EventName.Group_StepOut, this.onGroupStepOutHandler);
            Dispatcher.removeEventListener(EventName.ChangeScene, this.onGroupStepOutHandler);
            return;
        }

        /* internal  */bindEntity(): void
        {
            this.unBindEntity();
            if (data) 
            {
                if (EntityUtil.equal(Cache.instance.role.entityInfo.entityId, this._player.player.entityId)) 
                {
                    this._iEntity = RolePlayer.instance;
                }
                else 
                {
                    this._iEntity = ThingUtil.entityUtil.getEntity(this._player.player.entityId);
                }
                if (this._iEntity) 
                {
                    this._iEntity.addEventListener(PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
                }
            }
            return;
        }

        /* internal  */unBindEntity(): void
        {
            if (this._iEntity) 
            {
                this._iEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
                this._iEntity = null;
            }
            return;
        }

        /* internal  */onPlayerInfoUpdate(arg1: PlayerEvent): void
        {
            this.updateLife();
            return;
        }

        /* internal  */updateLife(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._iEntity) 
            {
                this._name.htmlText = HTMLUtil.addColor(this._player.player.name, CareerUtil.getCareerColorString(this._player.player.careerEnable));
                loc1 = this._iEntity.entityInfo.entityInfo;
                loc2 = Math.ceil(loc1.life / loc1.maxLife * 100) + "%";
                this._blood.htmlText = HTMLUtil.addColor(loc2, "#FFFFFF");
                this._greenBloodBm.width = 117 * loc1.life / loc1.maxLife;
            }
            else if (this._player.player.online != 0) 
            {
                this._name.htmlText = HTMLUtil.addColor(this._player.player.name, CareerUtil.getCareerColorString(this._player.player.careerEnable));
                this._blood.htmlText = HTMLUtil.addColor(Language.getString(60686), "#FFFFFF");
            }
            else 
            {
                this._greenBloodBm.width = 0;
                this._name.htmlText = HTMLUtil.addColor(this._player.player.name, GlobalStyle.gray);
                this._blood.htmlText = HTMLUtil.addColor(Language.getString(60685), GlobalStyle.gray);
            }
            return;
        }

        /* internal  */onGroupStepInHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityInfo;
            if (loc1) 
            {
                if (EntityUtil.equal(loc1.entityId, this._player.player.entityId)) 
                {
                    this.bindEntity();
                    this.updateLife();
                }
            }
            return;
        }

        /* internal  */onGroupStepOutHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            if (loc1 != null) 
            {
                if (EntityUtil.equal(loc1, this._player.player.entityId)) 
                {
                    this.bindEntity();
                    this.updateLife();
                }
            }
            else 
            {
                this.bindEntity();
                this.updateLife();
            }
            return;
        }

        /* internal */ /* var */_iEntity: IEntity;

        /* internal */ /* var */_greenBloodBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_name: GTextFiled;

        /* internal */ /* var */_blood: GTextFiled;

        /* internal */ /* var */_careerBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_player: SGuildGroupPlayer;
    }
